<? // loadpgn.php


//-$url = 'http://www.chessbase.com/news/2010/khanty/games/khanty01.pgn';
//-$url = 'www.chessbase.com/news/2010/khanty/games/khawnty01.pgn';

// strip http
$url = pure_url( $_GET['url'] );
if( strpos($url,':') > 0 ) $url = substr( $url, strpos($url,':')+3 );
//-echo "opening $url<br/>";

$ch = curl_init($url); 
if( !$ch ) ajax_exit( 'error', "cannot open $url" );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$pgn = curl_exec($ch); 
$info = curl_getinfo($ch);
curl_close($ch);
if( !$pgn  ||( $info['http_code'] && $info['http_code']!=200 ) ) {
	if( $info['http_code'] ) header( 'HTTP/1.0 '.(($info['http_code']==404)?'404 Not Found':"{$info['http_code']} Server Error") );
	ajax_exit( 'error', "unable to load pgn from $url" );
}

ajax_exit( 'success', 'pgn attached', array( 'url'=>$url, 'pgn'=>$pgn ) );

//-echo "pgn: $pgn<br/>";
//-echo "info: ";print_r($info); echo "<br/>";

exit;

?>
